package com.waypolice.manager.controller;

import com.waypolice.common.pojo.EasyUiDataGridResult;
import com.waypolice.manager.service.LogOperateService;
import com.waypolice.common.utils.DateTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;

/**
 * @Description 日志管理
 * @Author wangrang
 * @Date Created in 12:02 2018/10/10
 * @Modified By:
 */
@Controller
@Api(value = "日志管理", description = "操作日志管理")
@RequestMapping(value="/pcwindow")
public class LogOperateAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogOperateAction.class);

    @Autowired
    private LogOperateService logOperateService;

    /**
     * @author:wr
     * @description 跳转到日志管理页面
     * @date: 2018/9/1 9:46
     * @return
     */
    @ApiOperation(value = "跳转到日志管理页面")
    @RequestMapping("/log")
    public String toLogOperateMethod(){
        return "log/log_operate";
    }

    /**
     * 查询当天全部日志数据，倒序排序(分页查询,使用pageHelp分页插件)
     * @param page
     * @param rows
     * @return
     */
    @ApiOperation(value = "查询日志数据")
    @RequestMapping(value = "/findLog_list",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public EasyUiDataGridResult getLogList(HttpServletRequest request,
                                            HttpServletResponse response, Integer page, Integer rows) {
        //根据下拉框中选择的状态进行查询
        String dlzh_log = request.getParameter("dlzh_log");
        String ip_log = request.getParameter("ip_log");
        String startDate_log = request.getParameter("startDate_log");//开始日期
        String endDate_log = request.getParameter("endDate_log");//截止日期
        //建立分页数据对象
        EasyUiDataGridResult result = null;
        if(StringUtils.isBlank(dlzh_log) && StringUtils.isBlank(ip_log) &&
                StringUtils.isBlank(startDate_log) && StringUtils.isBlank(endDate_log)){
            result = logOperateService.findLogPageList(page, rows);
        }else {
            //根据条件查询
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            if(StringUtils.isNotBlank(startDate_log)){
                Date startDate = DateTimeUtil.strToDate(startDate_log);
                long time = startDate.getTime();
                objectObjectHashMap.put("startDate_log",time);
            }
            if(StringUtils.isNotBlank(endDate_log)){
                Date endDate = DateTimeUtil.strToDate(endDate_log);//截止日期
                long time2 = endDate.getTime();
                objectObjectHashMap.put("endDate_log",time2);
            }
            objectObjectHashMap.put("dlzh_log",dlzh_log);
            objectObjectHashMap.put("ip_log",ip_log);
            result = logOperateService.findLogPageList(objectObjectHashMap, page, rows);
        }
        return result;
    }

}
